<?php

namespace App\Http\Resources;

use App\Models\Entity;

use Illuminate\Http\Resources\Json\Resource;

class StudentUpdateResource extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $arr = [];

        foreach ($this->resource as $index => $item) {
            if($index == 0){
                $entity = Entity::query()->where('id', $item->entity_id)->first();
                $arr ['id'] = $item->id;
                    $arr ['twins'] = (int)$item->twins;
                    $arr ['is_twins'] = $item->is_twins == 0 ? false : true;
                    $arr ['section_id'] = (int)$item->section_id;
                    $arr ['family_address'] = (string)$entity->family_address;
                    $arr ['father_name'] = (string)$entity->father_name;
                    $arr ['father_type'] = (string)$entity->father_type;
                    $arr ['father_id_code'] = (string)$entity->father_id_code;
                    $arr ['father_works'] = (string)$entity->father_works;
                    $arr ['father_phone_number'] = (string)$entity->father_phone_number;
                    $arr ['mother_name'] = (string)$entity->mother_name;
                    $arr ['mother_type'] = (string)$entity->mother_type;
                    $arr ['mother_id_code'] = (string)$entity->mother_id_code;
                    $arr ['mother_works'] = (string)$entity->mother_works;
                    $arr ['mother_phone_number'] = (string)$entity->mother_phone_number;
            }
            $arr['children'][$index] = [
                'name' => (string)$item->name,
                'sex' => (string)$item->sex,
                'nation' => (string)$item->nation,
                'police_station_id' => (int)$item->police_station_id,
                'school_id' => (string)$item->school_id,
                'id_code' => (string)$item->id_code,
                'prophylactic_code' => (string)$item->prophylactic_code,
                'relation' => (string)$item->relation,
            ];
        }
            return $arr;
//                'children' => [[
//                    'name' => (string)$item->name,
//                    'sex' => (string)$item->sex,
//                    'nation' => (string)$item->nation,
//                    'police_station_id' => (int)$item->police_station_id,
//                    'school_id' => (string)$item->school_id,
//                    'id_code' => (string)$item->id_code,
//                    'prophylactic_code' => (string)$item->prophylactic_code,
//                    'relation' => (string)$item->relation,
//                ]],
//                'id' => $item->id,
//                'twins' => (int)$item->twins,
//                'section_id' => (int)$item->section_id,
//                'family_address' => (string)$item->getEntity($this->entity_id)->family_address,
//                'father_name' => (string)$item->getEntity($this->entity_id)->father_name,
//                'father_id_code' => (string)$item->getEntity($this->entity_id)->father_id_code,
//                'father_works' => (string)$item->getEntity($this->entity_id)->father_works,
//                'father_phone_number' => (string)$item->getEntity($this->entity_id)->father_phone_number,
//                'mother_name' => (string)$item->getEntity($this->entity_id)->mother_name,
//                'mother_id_code' => (string)$item->getEntity($this->entity_id)->mother_id_code,
//                'mother_works' => (string)$item->getEntity($this->entity_id)->mother_works,
//                'mother_phone_number' => (string)$item->getEntity($this->entity_id)->mother_phone_number
//            ];

    }
//    public function getEntity($id)
//    {
//        $entity = Entity::query()->where('id', $id)->first();
//        return $entity;
//    }
}
